package com.sitech.portal.panels.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sitech.BaseConst;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by liujhc on 1/12.
 */
public class LoginInfoAction extends ActionSupport {

    public TblSYS_LOGINMSG getCurrentUser() {
        return (TblSYS_LOGINMSG) ActionContext.getContext().getSession().get(BaseConst.SESSION_USER_KEY);
    }

    public int getSessionsSize() {
        Set<HttpSession> sessionSet = (Set<HttpSession>) ActionContext.getContext().getApplication().get("sessionSet");
        if (sessionSet == null) return 1;
        int size = 0;
        for (HttpSession session : new ArrayList<HttpSession>(sessionSet)) {
            try {
                // 5分钟内有活动 且是登录过的用户
                if (System.currentTimeMillis() - session.getLastAccessedTime() < 1000 * 60 * 5) {
                    size += 1;
                }
            } catch (Throwable ignored) {
            }
        }
        return size;
    }
}
